package cn.wolfcode.luowowo.website.web;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrtegyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrtegyStatisVOService strtegyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyEsService strategyEsService;

    @RequestMapping("")
    public String index(Model model){
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);

        //hotCds
        List<StrategyStatisVO> hotCds = strtegyStatisVOService.queryHotSortTop10();
        model.addAttribute("hotCds",hotCds);

        //abroadCds
        List<StrategyStatisVO> abroadCds = strtegyStatisVOService.queryAbroadSortTop10();
        model.addAttribute("abroadCds",abroadCds);

        //unabroadCds
        List<StrategyStatisVO> unabroadCds = strtegyStatisVOService.queryUnaBroadCdsSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);

        //主题推荐
        //themCds
        List<Map<String,Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds",data);

        //chinas
        List<StatisVO> chinas = strategyEsService.queryConditionChina();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads = strategyEsService.queryConditionAbroads();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVO> themes = strategyEsService.queryConditionThemes();
        model.addAttribute("themes",themes);
        //List<StatisVO> chinas = strategyEsService.
        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Model model , Long id,@UserParam UserInfo userInfo){
        //因为我们攻略详情和内容分表了,这里建议分开查询
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content =strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //阅读数的统计
        strtegyStatisVOService.increaseViewNum(id,1);
        //共享vo对象,回显出阅读数
        StrategyStatisVO vo  = strtegyStatisVOService.getStrategyStatisVo(id);
        //阅读:排行加分
        strtegyStatisVOService.addScoreHot(id,1);

        if(userInfo!=null){
            boolean isFavor = strtegyStatisVOService.getIsFavor(id,userInfo.getId());
            model.addAttribute("isFavor",isFavor);
        }
        model.addAttribute("vo",vo);
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo") StrategyDetailQuery qo){

        //toasts  吐司操作
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        System.out.println(qo.getDestId());
        System.out.println(toasts.size());
        //最后的目的地单独处理,吐司多余一个,所以我们删除最后一个
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",dest);
        //tags
        model.addAttribute("tags",strategyTagService.list());
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        strategyCommentService.save(comment);

        //评论数的添加
        strtegyStatisVOService.increaseReplyNum(comment.getDetailId(),1);
        //获取vo对象
        StrategyStatisVO vo = strtegyStatisVOService.getStrategyStatisVo(comment.getDetailId());

        //评论:排行加分
        strtegyStatisVOService.addScoreHot(comment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        //这里分页不能用之前的,异步分页
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        //判断是否登录
        if(userInfo==null){
            //没有登录
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);//没有登录的状态
            return result;
        }
        //取消收藏和收藏
        boolean ret = strtegyStatisVOService.favor(sid,userInfo.getId());
        StrategyStatisVO vo = strtegyStatisVOService.getStrategyStatisVo(sid);

        //收藏:国内外排行加分
        strtegyStatisVOService.addScoreAbroadAndChina(sid,ret?1:-1);
        return new AjaxResult(ret,"").addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        //判断是否登录
        if(userInfo==null){
            //没有登录
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);//没有登录的状态
            return result;
        }

        boolean ret = strtegyStatisVOService.thumbup(sid,userInfo.getId());
        StrategyStatisVO vo = strtegyStatisVOService.getStrategyStatisVo(sid);
        //点赞:国内外排行加分
        if (ret) {
            strtegyStatisVOService.addScoreAbroadAndChina(sid,1);
        }

        return new AjaxResult(ret,"").addData(vo);
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){
        Page page = strategyEsService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }
}
